import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-garbage-clear-detail',
	templateUrl: './garbage-clear-detail.component.html',
	styleUrls: ['./garbage-clear-detail.component.css']
})
export class GarbageClearDetailComponent implements OnInit {
	tableData = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	startTime_table:string = '';
	endTime_table:string = '';
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	carNumber:string = '';
	deviceId:string = '';
	timeType:string='1'; //时间类型
	initDateRange: any = {
        startTime:'',
        endTime:'',
    }// 时间组件需要渲染的值
    jumpData:any; // 跳转页面接收到的数据
    companyData:any = {
        companyId: '',
        companyName: '',
    }
	columnSortFlag:number;//table列 排序
	sortBy:number;
    sortType:number;
    carDeatailUseList = carTypeListObj.carDeatailUseList
	constructor(private jsonApiService: JsonApiService, private router: Router,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
			if(common.getCookie('jumpTo/environment-manage/garbage-clear-detail')){
                this.jumpData = JSON.parse(common.getCookie('jumpTo/environment-manage/garbage-clear-detail'));
                if(this.jumpData.startTime&&this.jumpData.endTime){
                    this.initDateRange.startTime = this.startTime_table = this.jumpData.startTime;
                    this.initDateRange.endTime = this.startTime_table = this.jumpData.endTime;
                }
                this.companyData.companyName = this.selectedCompanyName = this.jumpData.companyName;
                this.companyData.companyId = this.selectedCompanyId = this.jumpData.companyId;
                this.companyData.deviceId = this.deviceId = this.jumpData.deviceId;
                this.companyData.carNumber = this.carNumber = this.jumpData.carNumber;
            }
		});
	}

	ngOnInit() {
	    let that = this;
        if(common.getCookie('jumpTo/environment-manage/garbage-clear-detail')) {
            setTimeout(()=>{
                that.load();
            },100)
            common.clearCookie('jumpTo/environment-manage/garbage-clear-detail');
        }else{
            let date = new Date();
            date.setDate(date.getDate() - 1);
            let yersterday = common.getTimeDay(date);
            this.initDateRange.startTime = this.startTime_table = yersterday+' 00:00:00';
            this.initDateRange.endTime = this.endTime_table = yersterday+' 23:59:59';
            setTimeout(()=>{
                that.load();
            },100)
        }
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time){
		this.startTime_table = time.startTime;
		this.endTime_table = time.endTime;
	} 
	//公司选中回调
    companySelected(data){
        this.selectedCompanyId = data.companyId;
        this.selectedCompanyName = data.companyName;
        this.deviceId = data.deviceId;
        this.carNumber = data.carNumber;
    }
	//表格搜索
	table_search() {
	    this.columnSortFlag = null;
        this.sortBy = null;
        this.sortType = null;
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//刷新
	refresh(){
    	this.load();
    }
	// 列  排序
    columnSort(val){
        this.columnSortFlag = val;
        switch (val){
            case 3:
                this.sortBy = 0;
                this.sortType = 0;
                break;
            case 4:
                this.sortBy = 0;
                this.sortType = 1;
                break;
            case 5:
                this.sortBy = 1;
                this.sortType = 0;
                break;
            case 6:
                this.sortBy = 1;
                this.sortType = 1;
                break;
        }
        this.load();
    }
    getUseTypeNameL2(key) {
        if(key) {
            return this.carDeatailUseList[key];
        } else {
            return '';
        }
    }
	//初始化加载list数据
	load() {
	    if(!this.startTime_table||!this.endTime_table){
            return this.jsonApiService.message('起始或结束时间不能为空');
        }
        let timeCheck=common.correctTimeParams(this.startTime_table,this.endTime_table,60);
        if(!timeCheck.status){
            this.jsonApiService.message(timeCheck.message);
            return;
        }
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				companyId: this.selectedCompanyId,
				deviceId: this.deviceId,
				sortBy: this.sortBy, //sortBy 0:运输次数   1:运输桶数  2:总重量
				sortType: this.sortType, //sortType 排序类型 0:升序   1:降序
				startDate: this.startTime_table,
				endDate: this.endTime_table,
//				searchKey: this.searchKey
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("environmentalCarQingyunDetails/environmentalCarQingyunList.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
			for(let item of data.exhibitDatas) {
                item.useTypeL2 = this.getUseTypeNameL2(item.useTypeL2);
            }
		})
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//导出报表
	exportData() {
		if(!this.startTime_table||!this.endTime_table){
			return this.jsonApiService.message('起始或结束时间不能为空');
		}
		let timeCheck=common.correctTimeParams(this.startTime_table,this.endTime_table,60);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let parameters = {
				companyId: this.selectedCompanyId,
				deviceId: this.deviceId,
				sortBy: this.sortBy, //sortBy 0:运输次数   1:运输桶数  2:总重量
                sortType: this.sortType, //sortType 排序类型 0:升序   1:降序
				startDate: this.startTime_table,
                endDate: this.endTime_table,
//				searchKey: this.searchKey,
			};
		let paramsName = 'param';
		let _url = config.BaseURL + 'environmentalCarQingyunDetails/exportEnvironmentalCarQingyun.html';
		common.exprotDataFile(parameters,paramsName,_url);
	}
}